/*
 * GraficoLinhas.java
 * @author fabricio@utfpr.edu.br
 * Created on 02/09/2011, 15:17:39
 */
package graficos;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.RefineryUtilities;


/**
 * @author fabricio
 */
public class GraficoLinhas extends javax.swing.JFrame {//implements Formulario {

    /**
     * Creates new form GraficoLinhas
     */
    public GraficoLinhas() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("Plota Gráfico");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Cancelar");
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
        });
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(151, Short.MAX_VALUE)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap()));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                .addGap(125, 125, 125)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jButton2)
                .addComponent(jButton1))
                .addContainerGap(146, Short.MAX_VALUE)));

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width - 400) / 2, (screenSize.height - 325) / 2, 400, 325);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        if (dadosvalidos()) {
            clickconfirmar();
        } else {
            clickcancelar();
        }
    }//GEN-LAST:event_jButton1MouseClicked

    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked
        clickcancelar();
    }//GEN-LAST:event_jButton2MouseClicked

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    }//GEN-LAST:event_jButton1ActionPerformed

    public void LineChart(DefaultCategoryDataset dataset, String title,
            String x_axis_label, String y_axis_label, boolean showlegend,
            float maxvalue, float minvalue) {

        JFrame chartwindow = new JFrame(title);

        JFreeChart jfreechart = ChartFactory.createLineChart(
                title,
                x_axis_label,
                y_axis_label,
                dataset,
                PlotOrientation.VERTICAL,
                showlegend, // include legend
                true, // tooltips
                true // urls
                );

        CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot();
        categoryplot.setBackgroundPaint(Color.white);
        categoryplot.setRangeGridlinePaint(Color.black);
        NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
        numberaxis.setStandardTickUnits(NumberAxis.createStandardTickUnits());

        CategoryPlot plot = (CategoryPlot) jfreechart.getPlot();
        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();

        if (minvalue == 0 && maxvalue == 0) {
            rangeAxis.setAutoRangeIncludesZero(true);
        } else {
            rangeAxis.setRange(minvalue, maxvalue);
        }
        LineAndShapeRenderer lineandshaperenderer =
                (LineAndShapeRenderer) categoryplot.getRenderer();
        lineandshaperenderer.setBaseStroke(new BasicStroke(2.0f));
        lineandshaperenderer.setShapesVisible(true);
        lineandshaperenderer.setDrawOutlines(true);
        lineandshaperenderer.setUseFillPaint(true);
        lineandshaperenderer.setFillPaint(Color.white);

        //GradientPaint gp1 = new GradientPaint(0.0f, 0.0f, Color.MAGENTA, 0.0f, 0.0f, Color.MAGENTA);
        //lineandshaperenderer.setSeriesPaint(0, gp1);
        //ou
        lineandshaperenderer.setSeriesPaint(0, Color.RED);

        JPanel jpanel = new ChartPanel(jfreechart);
        jpanel.setPreferredSize(new Dimension(800, 600));
        chartwindow.setContentPane(jpanel);
        chartwindow.pack();
        RefineryUtilities.centerFrameOnScreen(chartwindow);
        chartwindow.setVisible(true);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GraficoLinhas().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    // End of variables declaration//GEN-END:variables

    public void clickconfirmar() {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        double x = 0;
        double y = 0;
        double b = 5;
        for (x = -10; x < 10; x++) {
            y = (1 * x + b);
            dataset.addValue(y, "a=1", String.valueOf(x));
            y = (30 * x + b);
            dataset.addValue(y, "a=30", String.valueOf(x));
            y = (-30 * x + b);
            dataset.addValue(y, "a=-30", String.valueOf(x));
            y = (45 * x + b);
            dataset.addValue(y, "a=45", String.valueOf(x));
            y = (-45 * x + b);
            dataset.addValue(y, "a=-45", String.valueOf(x));
            y = (90 * x + b);
            dataset.addValue(y, "a=90", String.valueOf(x));
        }
        LineChart(dataset, "line", "axis x", "axis y", true, 0, 0);
    }

    public void clickcancelar() {
        System.exit(0);
    }

    public boolean dadosvalidos() {
        java.awt.Dimension resolucao =
                java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        int altura = 680;
        int largura = 480;
        if (altura <= resolucao.getHeight()
                && largura <= resolucao.getWidth()) {
            return true;
        } else {
            JOptionPane.showMessageDialog(
                    this,
                    "erro",
                    "Titulo-Erro",
                    JOptionPane.ERROR_MESSAGE);
            return false;
        }
    }
}